"""
This module contains the Kiwi class.
"""

from qnd import *

####CONSTANTS####
GRAVITY = .22
MAXFALLSPEED = 16
JUMPSPEED = 8

####CLASS####
class Kiwi:
	"""
	This is the class that controls the main character. Call update()
	every frame, and draw() at the end of every frame too.
	"""
	sprite = load_image('kiwi.png')
	def __init__(self, (x,y)):
		self.x,self.y=x,y
		self.speed = 5
		self.yspeed = 0
	def update(self,input,boxes):
		"""Called every frame"""
		
		onfloor = self.check_floor(boxes)
		
		if not onfloor:
			if self.yspeed < MAXFALLSPEED:
				self.yspeed += GRAVITY
		else:
			if self.yspeed > 0:
				self.yspeed = 0
				
		if input.keys[K_UP] and onfloor:
			self.yspeed = -JUMPSPEED
		
		self.y += self.yspeed
		
		if input.keys[K_LEFT]:
			self.x -= self.speed
		if input.keys[K_RIGHT]:
			self.x += self.speed
	def check_floor(self,boxes):
		"""
		Returns True if the Kiwi is standing on any of the boxes in
		boxes.
		"""
		floor = False
		kiwix,kiwiy = self.x,self.y
		for box in boxes:
			if box.x > kiwix-31 and box.x < kiwix +63 and box.y > kiwiy +90 and box.y < kiwiy +100:
				floor = True
				if self.yspeed > 0:
					self.y = box.y-96
				break
		return floor
	def draw(self,screen,camera):
		draw_image(screen,self.sprite,(self.x,int(self.y)))
